$(function() {
				var $form_div = $("#zbdailyTotal_form div");
				winResize($form_div);
				$(window).resize(function(){//当调整浏览器窗口的大小时，发生 resize 事件
					winResize($form_div);
				});
				loadTable();
				
})


var $form = $('#zbdailyTotal_form');

//指定列求和
function compute_GXC(colName) {
     var rows = $('#zbdailyTotal_datagrid').datagrid('getRows');
     var total = 0;
     for (var i = 0; i < rows.length; i++) {
    	 if(typeof(rows[i][colName]) != 'undefined'){
    		 total += parseFloat(rows[i][colName]);
    	 }
     }
     if(total > 0){
    	 return total.toFixed(1);
     }else{
    	 return 0;
     }
}


function  loadTable()
{	
	var columnsAll = new Array();
	columnsAll.push({field:'ids',title:'ids',align:'center',rowspan:2,hidden:true});
	
	columnsAll.push({field:'cjrs',title:'新增人数',align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		},rowspan:2});
	columnsAll.push({field:'lc001salnum',title:'疗程数',width:50,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		},rowspan:2});
	columnsAll.push({field:'fwcs',title:'服务次数',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	columnsAll.push({field:'dcrs',title:'掉秤人数',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	columnsAll.push({field:'dcjs',title:'掉秤斤数',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	columnsAll.push({field:'zcrs',title:'涨称人数',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	columnsAll.push({field:'zcjs',title:'涨称斤数',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	columnsAll.push({field:'pcrs',title:'平称人数',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	columnsAll.push({field:'zjz',title:'总减重',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			var dcjs = parseFloat(0);
			var zcjs = parseFloat(0);
			if(row.dcjs!=null && row.dcjs!=""){
				dcjs = parseFloat(row.dcjs);
			}
			
			if(row.zcjs!=null && row.zcjs!=""){
				zcjs = parseFloat(row.zcjs);
			}
				if((dcjs-zcjs) == "0"){
					return "";
				}else{
					return   parseFloat(dcjs-zcjs).toFixed(1);
				}
				
		}
		
	});

	columnsAll.push({field:'sycprs',title:'精护人数',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	columnsAll.push({field:'ggrs',title:'巩固人数',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	columnsAll.push({field:'dbrs',title:'达标人数',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	columnsAll.push({field:'rjdccc',title:'人均<br/>掉秤',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value!=null){
				return value;
			}else{
				var zjz = parseFloat(0);
				var dcjs = parseFloat(0);
				var zcjs = parseFloat(0);
				if(row.dcjs!=null && row.dcjs!=""){
					dcjs = parseFloat(row.dcjs);
				}
				
				if(row.zcjs!=null && row.zcjs!=""){
					zcjs = parseFloat(row.zcjs);
				}
					
				zjz =  parseFloat(dcjs-zcjs).toFixed(1);
			
				
				var ggrs = parseFloat(0);
				var sddrs = parseFloat(0);
				
				if(row.zjz!=null && row.zjz!=""){
					zjz = parseFloat(row.zjz);
				}
				
				if(row.ggrs!=null && row.ggrs!=""){
					ggrs = parseFloat(row.ggrs);
				}
				
				if(row.fwcs!=null && row.fwcs!=""){
					sddrs = parseFloat(row.fwcs);
				}
				var rjdc =0;
				if((sddrs-ggrs)!=0){
					rjdc = zjz/(sddrs-ggrs);
					
				}
				if(rjdc == "0"){
					return "";
				}else{
					return  rjdc.toFixed(1);
				}
					
			}
			
		}
	});
	columnsAll.push({field:'dcrszb',title:'掉秤<br/>占比',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value!=null){ 
				return  value+"%";
			}else{
				var dcrs = parseFloat(row.dcrs);
				var ggrs = parseFloat(row.ggrs);
				var dcbl = 0;
				var fwcs = parseFloat(row.fwcs);
				if((fwcs-ggrs)!=0){
					dcbl = parseFloat(dcrs/(fwcs-ggrs)*100);
					dcbl = dcbl.toFixed(2);
					dcbl = dcbl+'%';
				}
				return dcbl;
			}
		}
	});
	columnsAll.push({field:'jhzb',title:'精护<br/>占比',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value!=null){
				return value;
			}else{
				
				var sycprs =   parseFloat(0);
				var sddrs = parseFloat(0);
				if(row.fwcs!=null && row.fwcs!=""){
					sddrs = parseFloat(row.fwcs);
				}
				if(row.sycprs!=null && row.sycprs!=""){
					sycprs = parseFloat(row.sycprs);
				}
				if(sddrs!=0){
				
					var value = parseFloat(sycprs)/sddrs*100;
					value = value.toFixed(2)+"%";
				}
				
				return  value;
			}
				
			
		}
	});
	columnsAll.push({field:'jzzxse',title:'减重销售',width:65,rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	columnsAll.push({field:'cpxs',title:'产品销售',colspan:10,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	
	columnsAll.push({field:'pricetotal',title:'总销售',width:70,rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	
	
	
	
	
	
	var columnsAll1 = new Array();
	
	
	columnsAll1.push({field:'hf001salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'hf002salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'hf003salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'hf004salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'hf005salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'hf011salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'ss001salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'ss002salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'jyg',title:'精油、膏',width:65,align:'center',
		formatter: function(value,row,index)
		{	
			var jyg = parseFloat(row.ss001salprice)+parseFloat(row.ss002salprice);
			if(jyg == "0"){
				return "";
			}else{
				return  jyg.toFixed(1);
			}
			
		}
	});
	columnsAll1.push({field:'qt',title:'其他产品',width:60,align:'center',
		formatter: function(value,row,index)
		{	
			var qt = parseFloat(row.hf001salprice)+parseFloat(row.hf002salprice)
			+parseFloat(row.hf003salprice)
			+parseFloat(row.hf004salprice)
			+parseFloat(row.hf005salprice)
			+parseFloat(row.hf011salprice);
			if(qt == "0"){
				return "";
			}else{
				return  qt.toFixed(1);
			}
			
		}
	});
	
	
	$('#zbdailyTotal_datagrid').datagrid({
		cls: "theme-datagrid",
		singleSelect:true,
		fitColumns:true,
		rownumbers: true,

		frozenColumns:[[
		                {field:'provincename',title:'名称',align:'center',
							formatter: function(value,row,index){
								
								var ids = row.province;
							return '<a href="#" style="color: blue;" class="button-edit" onclick="lookInfo(\'' + ids + '\')">' + value + '</a>';
						}}
		                ]],
	    columns:[columnsAll,columnsAll1],
	    url: "zbDailyTotalStatistic/getZbDailyTotal",
	    queryParams: sy.serializeObject($form),
	    onLoadSuccess:function(data){
	    	var n = data.total;
			 var rows = $('#zbdailyTotal_datagrid').datagrid('getRows');
			if(rows.length > 0){
				var hjddrs = compute_GXC('fwcs');
				var hjdcjs =compute_GXC('dcjs');
				var hjzcjs =compute_GXC('zcjs');
				var hjzjz =hjdcjs-compute_GXC('zcjs');
				var hjggrs = compute_GXC('ggrs');
				var hjdcrs = compute_GXC('dcrs');
				var hjsycprs = compute_GXC('sycprs');
				var cjrs = compute_GXC('cjrs');
				var lc001salnum = compute_GXC('lc001salnum');
				var fwcs =  compute_GXC('fwcs');
				var dcrs=compute_GXC('dcrs');
				var dcjs=compute_GXC('dcjs');
				var zcrs=compute_GXC('zcrs');
				var zcjs=compute_GXC('zcjs');
				var pcrs=compute_GXC('pcrs');
				var zjz=compute_GXC('zjz');
				var sycprs=compute_GXC('sycprs');
				var ggrs=compute_GXC('ggrs');
				var dbrs=compute_GXC('dbrs');
				var jzzxse=compute_GXC('jzzxse');
				var hf001salnum=compute_GXC('hf001salnum');
				var hf001salprice=compute_GXC('hf001salprice');
				var hf002salnum=compute_GXC('hf002salnum');
				var hf002salprice=compute_GXC('hf002salprice');
				var hf003salnum=compute_GXC('hf003salnum');
				var hf003salprice=compute_GXC('hf003salprice');
				var hf004salnum=compute_GXC('hf004salnum');
				var hf004salprice=compute_GXC('hf004salprice');
				var hf005salnum=compute_GXC('hf005salnum');
				var hf005salprice=compute_GXC('hf005salprice');
				var hf011salnum=compute_GXC('hf011salnum');
				var hf011salprice=compute_GXC('hf011salprice');
				
				var ss001salnum=compute_GXC('ss001salnum');
				var ss001salprice=compute_GXC('ss001salprice');
				var ss002salnum=compute_GXC('ss002salnum');
				var ss002salprice=compute_GXC('ss002salprice');
				
				
				var pricetotal=compute_GXC('pricetotal')
				
				
				
				
				var hjrjdc = 0;
				var hjdczb = 0;
				
				if(hjddrs-hjggrs!= 0){
					
					hjrjdc = hjzjz/(parseFloat(hjddrs)-parseFloat(hjggrs));
					hjrjdc = hjrjdc.toFixed(2);
				
					hjdczb = hjdcrs/(parseFloat(hjddrs)-parseFloat(hjggrs))*100;
					hjdczb = hjdczb.toFixed(2);
				}
				if(hjddrs!=0){
					
					hjsycprs = hjsycprs/hjddrs*100;
					hjsycprs = hjsycprs.toFixed(2);
					hjsycprs = hjsycprs+'%'
				}
				$('#zbdailyTotal_datagrid').datagrid('insertRow', {
					index:0,
					row:{provincename: '合计',
					cjrs:cjrs,
					lc001salnum:lc001salnum,
					fwcs:fwcs,
					dcrs:dcrs,
					dcjs:dcjs,
					zcrs:zcrs,
					zcjs:zcjs,
					pcrs:pcrs,
					zjz:zjz,
					sycprs:sycprs,
					ggrs:ggrs,
					dbrs:dbrs,
					rjdccc:hjrjdc,
					dcrszb:hjdczb,
					jhzb:hjsycprs,
					jzzxse:jzzxse,
					hf001salnum:hf001salnum,
					hf001salprice:hf001salprice,
					hf002salnum:hf002salnum,
					hf002salprice:hf002salprice,
					hf003salnum:hf003salnum,
					hf003salprice:hf003salprice,
					hf004salnum:hf004salnum,
					hf004salprice:hf004salprice,
					hf005salnum:hf005salnum,
					hf005salprice:hf005salprice,
					hf011salnum:hf011salnum,
					hf011salprice:hf011salprice,
					
					ss001salnum:ss001salnum,
					ss001salprice:ss001salprice,
					ss002salnum:ss002salnum,
					ss002salprice:ss002salprice,
					
					
					pricetotal:pricetotal
					}
	             });
				
				$('#zbdailyTotal_datagrid').datagrid('insertRow', {
					index:1,
					row:{provincename: '均值',
						cjrs:(cjrs/n).toFixed(1),
						lc001salnum:(lc001salnum/n).toFixed(1),
						fwcs:(fwcs/n).toFixed(1),
						dcrs:(dcrs/n).toFixed(1),
						dcjs:(dcjs/n).toFixed(1),
						zcrs:(zcrs/n).toFixed(1),
						zcjs:(zcjs/n).toFixed(1),
						pcrs:(pcrs/n).toFixed(1),
						zjz:(zjz/n).toFixed(1),
						sycprs:(sycprs/n).toFixed(1),
						ggrs:(ggrs/n).toFixed(1),
						dbrs:(dbrs/n).toFixed(1),
						rjdccc:hjrjdc,
						dcrszb:hjdczb,
						jhzb:hjsycprs,
						jzzxse:(jzzxse/n).toFixed(1),
						hf001salnum:(hf001salnum/n).toFixed(1),
						hf001salprice:(hf001salprice/n).toFixed(1),
						hf002salnum:(hf002salnum/n).toFixed(1),
						hf002salprice:(hf002salprice/n).toFixed(1),
						hf003salnum:(hf003salnum/n).toFixed(1),
						hf003salprice:(hf003salprice/n).toFixed(1),
						hf004salnum:(hf004salnum/n).toFixed(1),
						hf004salprice:(hf004salprice/n).toFixed(1),
						hf005salnum:(hf005salnum/n).toFixed(1),
						hf005salprice:(hf005salprice/n).toFixed(1),
						hf011salnum:(hf011salnum/n).toFixed(1),
						hf011salprice:(hf011salprice/n).toFixed(1),
						
						ss001salnum:(ss001salnum/n).toFixed(1),
						ss001salprice:(ss001salprice/n).toFixed(1),
						ss002salnum:(ss002salnum/n).toFixed(1),
						ss002salprice:(ss002salprice/n).toFixed(1),
						
						
						pricetotal:(pricetotal/n).toFixed(1)
					}
	             });
		
			} 
		}
	});
	
}

//查看门店报表详细信息
function lookInfo(ids){
	loadViewDialog('查看详细信息', '/shouba2.0/zbDailyTotalStatistic/tolookInfo?organids=' + ids);
}

function searchFrom(){
//	loadTable();
	$('#zbdailyTotal_datagrid').datagrid('load',sy.serializeObject($form));
}


